package hedron.editor;

import java.text.DecimalFormat;

public class EditorUtils {

	public static String flatString(String str) {
		if (str == null || str.length() <= 0) {
			return "";
		}
		StringBuilder result = new StringBuilder(str.length());
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c == '\n') {
				result.append(", ");
			} else if (c == '\r') {} else {
				result.append(c);
			}
		}
		return result.toString();
	}

	public static String formString(String str) {
		if (str == null || str.length() <= 0) {
			return "";
		}
		StringBuilder result = new StringBuilder(str.length() + 10);
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c == '<') {
				result.append("&lt;");
			} else if (c == '>') {
				result.append("&gt;");
			} else if (c == '&') {
				result.append("&amp;");
			} else if (c == '\n') {
				result.append("<br/>");
			} else if (c == '\r') {} else {
				result.append(c);
			}
		}
		return result.toString();
	}

	static final DecimalFormat numberFormater;
	static {
		numberFormater = new DecimalFormat();
		numberFormater.applyPattern("##########0.0000;-###########0.0000");
	}

	public static String formatDouble(double dval) {
		return numberFormater.format(dval);
	}
}
